package com.codechuan.demo.adapter;

import static android.os.Looper.getMainLooper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.codechuan.demo.R;
import com.codechuan.demo.common.Constant;
import com.codechuan.demo.dto.SuccessOrder;
import com.codechuan.demo.ui.myOrder.MyOrder;
import com.codechuan.demo.utils.DialogUtil;
import com.codechuan.demo.utils.HttpControllerUtil;
import com.codechuan.demo.utils.MyHttpUtil;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.util.List;

/**
 * @Deprecated 订单适配器
 */
public class OrderBaseAdapter extends BaseAdapter {
    private Handler handler;

    private Context context;

    private List<SuccessOrder> successOrders;

    private BottomSheetDialog bottomSheetView;

    public OrderBaseAdapter(Context context, List<SuccessOrder> successOrders,BottomSheetDialog bottomSheetView) {
        this.context = context;
        this.successOrders = successOrders;
        this.bottomSheetView = bottomSheetView;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }


    @Override
    public int getCount() {
        // 获取总回显数
        return successOrders.size();
    }

    @Override
    public Object getItem(int i) {
        return successOrders.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        // 获取主线程
        handler = new Handler(getMainLooper());
        OrderViewHolder viewHolder;
        if (view == null) {
            // 获取布局样式内容,根据布局***.xml 生成视图对象
            view = LayoutInflater.from(context).inflate(R.layout.item_order, null);
            viewHolder = new OrderViewHolder();
            viewHolder.ll_item = view.findViewById(R.id.order_ll_item);
            viewHolder.order = view.findViewById(R.id.order_id);
            viewHolder.price = view.findViewById(R.id.order_money);
            viewHolder.desc = view.findViewById(R.id.order_des);
            viewHolder.person = view.findViewById(R.id.order_person);
            viewHolder.orderType = view.findViewById(R.id.order_type);
            viewHolder.button = view.findViewById(R.id.order_click_button);
            viewHolder.orderInfoButton = view.findViewById(R.id.order_click_info_button);
            viewHolder.deleteOrder = view.findViewById(R.id.delte_order);
            viewHolder.jumpButton = view.findViewById(R.id.jump);
            view.setTag(viewHolder);
        } else {
            viewHolder = (OrderViewHolder) view.getTag();
        }

        //  blocksDescendants 阻止下面的控件获得焦点
        viewHolder.ll_item.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

        SuccessOrder successOrder = successOrders.get(i);
        // 设置order回显格式
        String tokenName = "订单" + (i + 1) + ":" + maskString(successOrder.getPaxJourneyGuid());
        viewHolder.order.setText(tokenName);

        String priceName = successOrder.getPrice() + "元";
        viewHolder.price.setText(priceName);

        String descName = "全程:" + successOrder.getOrderDes() + "公里";
        viewHolder.person.setText(descName);

        String typeName = successOrder.getPersonNumber() + "人" + successOrder.getCarPoolText();
        viewHolder.desc.setText(typeName);

        String orderTypeName = "接单类型:" + successOrder.getOrderType() + "订单";
        viewHolder.orderType.setText(orderTypeName);
        // 详情按钮点击事件
        viewHolder.button.setOnClickListener(view1 -> {
            String resultMsg;
            resultMsg = successOrder.getMsg();
            DialogUtil.showDialog(Constant.context, resultMsg == null ? "状态:订单参数异常" : "状态" + resultMsg);
        });
        // PK信息点击事件
        viewHolder.orderInfoButton.setOnClickListener(view1 -> {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    if (Constant.cityInfo == null) {
                        handler.post(() -> DialogUtil.showDialog(Constant.context, "请先定位城市."));
                        return;
                    }
                    String pkInfo = HttpControllerUtil.getPkInfo(successOrder.getPaxJourneyGuid());
                    if (pkInfo != null && pkInfo.equals("0")) {
                        handler.post(() -> DialogUtil.showDialog(Constant.context, "PK中请等待."));
                    }
                    if (pkInfo != null && pkInfo.equals("100000")) {
                        String weChatMiniMyOrderOrMyScheduleOrder = HttpControllerUtil.getWeChatMiniMyOrderOrMyScheduleOrder();

                        JSONObject parse = null;
                        try {
                            parse =JSONObject.parseObject(weChatMiniMyOrderOrMyScheduleOrder);
                        }catch (Exception e){
                            Constant.handler.post(() -> DialogUtil.showDialog(Constant.context, "哈喽官方提示:" + weChatMiniMyOrderOrMyScheduleOrder));
                            return;
                        }

                        // 判断接口是不是异常
                        if (weChatMiniMyOrderOrMyScheduleOrder == null){
                            handler.post(() -> DialogUtil.showDialog(Constant.context, "查询失败,请重新重新."));
                            return;
                        }
                        // 判断数组有没有数据
                        JSONArray data = parse.getJSONArray("data");
                        if (data.isEmpty()) {
                            handler.post(() -> DialogUtil.showDialog(Constant.context, "PK失败."));
                            return;
                        }
                        for (Object datum : data) {
                            JSONObject jsonObject = JSONObject.parseObject(datum.toString());
                            // 获取和当前选择的id一样的子订单数据
                            if (Constant.SCHEDULE_ID == null && successOrder.getOrderType().equals("XCX")) {
                                handler.post(() -> DialogUtil.showDialog(Constant.context, "小请前往行程管理选中一个行程再查看结果."));
                                return;
                            }
                            if (successOrder.getOrderType().equals("XCX")) {
                                if (jsonObject.getString("driverJourneyGuid").equals(Constant.SCHEDULE_ID)) {
                                    JSONArray paxOrderList = jsonObject.getJSONArray("paxOrderList");
                                    if (paxOrderList.isEmpty()) {
                                        handler.post(() -> DialogUtil.showDialog(Constant.context, "您未PK成功."));
                                    } else {
                                        boolean flag = false;
                                        for (Object o : paxOrderList) {
                                            JSONObject jsonObject1 = JSONObject.parseObject(o.toString());
                                            if (jsonObject1.getString("passengerJourneyGuid").equals(successOrder.getPaxJourneyGuid())) {
                                                flag = true;
                                            }
                                        }
                                        if (flag) {
                                            handler.post(() -> DialogUtil.showDialog(Constant.context, "更新您PK成功,请开始接乘客吧!"));
                                        } else {
                                            handler.post(() -> DialogUtil.showDialog(Constant.context, "您未PK成功"));
                                        }
                                    }
                                } else {
                                    handler.post(() -> DialogUtil.showDialog(Constant.context, "当前订单行程与您,选择接单的行程不符,请选择正确的行程."));
                                    return;
                                }
                            } else {
                                JSONArray paxOrderList = jsonObject.getJSONArray("paxOrderList");
                                if (paxOrderList.isEmpty()) {
                                    handler.post(() -> DialogUtil.showDialog(Constant.context, "您未PK成功."));
                                } else {
                                    boolean flag = false;
                                    for (Object o : paxOrderList) {
                                        JSONObject jsonObject1 = JSONObject.parseObject(o.toString());
                                        if (jsonObject1.getString("passengerJourneyGuid").equals(successOrder.getPaxJourneyGuid())) {
                                            flag = true;
                                        }
                                    }
                                    if (flag) {
                                        handler.post(() -> DialogUtil.showDialog(Constant.context, "恭喜您PK成功,请开始接乘客吧!"));
                                    } else {
                                        handler.post(() -> DialogUtil.showDialog(Constant.context, "您未PK成功"));
                                    }
                                }
                            }
                        }
                    }
                }
            }).start();
        });
        // 删除订单
        viewHolder.deleteOrder.setOnClickListener(view13 -> new Thread(new Runnable() {
            @Override
            public void run() {
                boolean b = MyHttpUtil.deleteOrderInfo(successOrder.getPaxJourneyGuid());
                if (b) {
                    handler.post(() -> DialogUtil.showDialog(Constant.context, "删除成功,请返回上级页面重新获取."));
                }
            }
        }).start());
        // 弹出层
        viewHolder.jumpButton.setOnClickListener(view12 -> {

           // bottomSheetView.show();
            MyOrder.wc(successOrder.getStartAddress(),successOrder.getEndAddress()
                    ,successOrder.getPassengerName(),successOrder.getStartDes());
        });
        return view;
    }

    public static String maskString(String str) {
        if (str == null || str.length() <= 10) {
            return str;
        }
        return str.substring(0, 8) + "******";
    }

    public static final class OrderViewHolder {
        public LinearLayout ll_item;

        public TextView order;

        public TextView price;

        public TextView desc;

        public TextView person;
        public TextView orderType;

        public Button button;
        public Button orderInfoButton;
        public Button deleteOrder;
        public Button jumpButton;

    }
}
